﻿using System;
using System.Management;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EmbDocumentInhalatorSetup
{
    public class IiisApplicationPoolManager
    {
        /// <summary>
        /// Stops the IIS application pool.
        /// </summary>
        /// <param name="appPool">The app pool.</param>
        /// <returns></returns>
        /// This element was inserted by injac on machine PLATINCHECKER.
        /// 08.02.2011,16:29
        public static bool StopIisApplicationPool(string appPool)
        {
            ManagementBaseObject outParams;

            try
            {
                ManagementObject classInstance =
                    new ManagementObject("root\\MicrosoftIISv2",
                                         "IIsApplicationPool.Name='W3SVC/APPPOOLS/" + appPool + "'",
                                         null);

               
                // Execute the method and obtain the return values.
                outParams =
                    classInstance.InvokeMethod("Stop", null, null);

                return true;
            }catch(ManagementException ex)
            {
                throw;
            }

        }

        /// <summary>
        /// Starts the IIS application pool.
        /// </summary>
        /// <param name="appPool">The app pool.</param>
        /// <returns></returns>
        /// This element was inserted by injac on machine PLATINCHECKER.
        /// 08.02.2011,16:29
        public static bool StartIisApplicationPool(string appPool)
        {
                ManagementBaseObject outParams;

                try
                {
                    ManagementObject classInstance =
                        new ManagementObject("root\\MicrosoftIISv2",
                                             "IIsApplicationPool.Name='W3SVC/APPPOOLS/" + appPool + "'",
                                             null);


                    // Execute the method and obtain the return values.
                    outParams =
                        classInstance.InvokeMethod("Start", null, null);

                    return true;
                }
                catch (ManagementException ex)
                {
                    throw;
                }
            }

        /// <summary>
        /// Recycles the IIS application pool.
        /// </summary>
        /// <param name="appPool">The app pool.</param>
        /// <returns></returns>
        /// This element was inserted by injac on machine PLATINCHECKER.
        /// 08.02.2011,16:29
        public static bool RecycleIisApplicationPool(string appPool)
        {
            ManagementBaseObject outParams;

            try
            {
                ManagementObject classInstance =
                    new ManagementObject("root\\MicrosoftIISv2",
                                         "IIsApplicationPool.Name='W3SVC/APPPOOLS/" + appPool + "'",
                                         null);


                // Execute the method and obtain the return values.
                outParams =
                    classInstance.InvokeMethod("Recycle", null, null);

                return true;
            }
            catch (ManagementException ex)
            {
                throw;
            }
        }

        /// <summary>
        /// Checks the IIS application pool status.
        /// </summary>
        /// <param name="appPool">The app pool.</param>
        /// <returns></returns>
        /// This element was inserted by injac on machine PLATINCHECKER.
        /// 08.02.2011,17:42
        public static int CheckIisApplicationPoolStatus(string appPool)
        {
            try
            {
                ManagementObject classInstance =
                    new ManagementObject("root\\WebAdministration",
                    "ApplicationPool.Name='"+appPool+"'",
                    null);

                // no method in-parameters to define


                // Execute the method and obtain the return values.
                ManagementBaseObject outParams = 
                    classInstance.InvokeMethod("GetState", null, null);

                // List outParams
                return Convert.ToInt32(outParams["ReturnValue"].ToString());
            }
            catch(ManagementException err)
            {
                throw;
            }
        }

    }
}
